package com.ychen.ruijee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ychen.ruijee.bean.Category;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ychen@gotop.cn
 * @date 2024-05-31 0:32
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> saveCategory(@RequestBody Category category){
        log.info("要添加的菜品或套餐的信息为：{}",category);
        boolean saveFlag = categoryService.save(category);
        return saveFlag ? R.success("添加成功"):R.error("添加失败");
    }

    /**
     * 菜品分类查询展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> categoryPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        Page<Category> result = categoryService.categoryPage(page,pageSize);
        return R.success(result);

    }

    /**
     * 删除菜品 信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> deleteCategory(@PathVariable Long id){
        log.info("要删除的套餐的ID 是：{}",id);
        boolean flag = categoryService.deleteCategory(id);
        return flag ? R.success("套餐删除成功") : R.error("套餐删除失败");
    }

    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        log.info("要修改的 套餐信息为 ：{}",category.toString());
        boolean flag = categoryService.updateById(category);
        return flag ? R.success("套餐信息修改成功"):R.error("套餐信息修改失败");
    }

    @GetMapping("/list")
    public R<List<Category>> categoryList (Integer type){
        // 根据 type 查询类别
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null,Category::getType,type);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
